import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import mysql.JDBC;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JPasswordField;


public class LoginJFrame extends JFrame implements MouseListener {

    public LoginJFrame(){
        initLoginJFrame();
    }
    JPasswordField passwordField = new JPasswordField(10);
    JTextField accountField = new JTextField(10);
    private void initLoginJFrame() {
        this.setSize(800, 600);
        this.setTitle("图书管理系统 v1.0");
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel containerPanel = new JPanel();
        containerPanel.setLayout(new GridBagLayout());

        JPanel leftPanel = new JPanel();
        Color blue = new Color(9, 126, 206);
        leftPanel.setBackground(blue);
        leftPanel.setLayout(new BorderLayout());
        JLabel titleLabel = new JLabel("图书管理系统");
        Font font = new Font("仿宋", Font.BOLD, 28);
        titleLabel.setFont(font);
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        leftPanel.add(titleLabel, BorderLayout.CENTER);

        JPanel rightPanel = new JPanel(new GridBagLayout());
        rightPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        JLabel Textone = new JLabel("图书管理系统-登录");
        Font fontone = new Font("仿宋", Font.PLAIN, 25);
        Textone.setFont(fontone);
        Textone.setBounds(495,50,400,200);
        Textone.setBackground(Color.BLACK);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        containerPanel.add(leftPanel, gbc);

        gbc.gridx = 1;
        containerPanel.add(rightPanel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0; // 设置垂直方向上的权重为0，不会拉伸组件
        gbc.insets = new Insets(0, 0, 30, 0); // 添加间距
        Textone.setHorizontalAlignment(SwingConstants.CENTER);
        rightPanel.add(Textone, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0; // 设置垂直方向上的权重为0，不会拉伸组件
        gbc.fill = GridBagConstraints.HORIZONTAL; // 使用水平填充
        gbc.insets = new Insets(10, 20, 0, 20); // 添加间距
        rightPanel.add(new JLabel("账号:"), gbc);

        gbc.gridy = 2;
        gbc.weighty = 0.0;
        accountField.setPreferredSize(new Dimension(80, 30)); // 设置输入框的首选大小
        rightPanel.add(accountField, gbc);

        gbc.gridy = 3;
        rightPanel.add(new JLabel("密码:"), gbc);

        gbc.gridy = 4;
        gbc.weighty = 0.0;
        passwordField.setPreferredSize(new Dimension(80, 30)); // 设置输入框的首选大小
        rightPanel.add(passwordField, gbc);

        gbc.gridy = 5;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(15, 20, 0, 20); // 添加间距
        JButton loginButton = new JButton("登录");
        loginButton.setOpaque(true);
        loginButton.setForeground(Color.white);
        loginButton.setBackground(new Color(0x3C8BE5));
        loginButton.setBorderPainted(false);
        loginButton.addMouseListener(this);
        loginButton.setPreferredSize(new Dimension(100, 30)); // 设置按键的首选大小
        rightPanel.add(loginButton, gbc);

        this.setContentPane(containerPanel);
        this.setVisible(true);

    }
    public void getalluser(ArrayList<User> UserList){
        try {
            // 获取数据库连接
            Connection connection = JDBC.getConnection();
            System.out.println("数据库连接成功");
            int count = 0;
            // 在这里执行数据库操作
            Statement statement = connection.createStatement();
            String sql = "SELECT * FROM user";
            ResultSet resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                int id = resultSet.getInt("id");
                String groups = resultSet.getString("groups");
                String name = resultSet.getString("name");
                String gender = resultSet.getString("gender");
                int id_card = resultSet.getInt("id_card");
                String phone = resultSet.getString("phone");
                String identity = resultSet.getString("identity");
                int book_count = resultSet.getInt("book_count");
                int state = resultSet.getInt("state");
                User user = new User(id,groups,name,username,password,gender,id_card,phone,identity,book_count,state);
                UserList.add(user);
                System.out.println("已将"+count+"条数据加入uselist数组");
                count++;
            }
            resultSet.close();
            statement.close();
        } catch (ClassNotFoundException ex) {
            System.out.println("找不到 MySQL JDBC 驱动程序");
            ex.printStackTrace();
        } catch (SQLException ex) {
            System.out.println("数据库连接失败");
            ex.printStackTrace();
        }


    }
    @Override
    public void mouseClicked(MouseEvent e) {
        String id = accountField.getText();
        String password = new String(passwordField.getPassword());
        User user = new User();
        int count = 1;

        try {
            // 获取数据库连接
            Connection connection = JDBC.getConnection();
            System.out.println("数据库连接成功");
            // 在这里执行数据库操作
            Statement statement = connection.createStatement();
            String sql = "SELECT * FROM user";
            ResultSet resultSet = statement.executeQuery(sql);
            ArrayList<User> userlist = new ArrayList<>();
            while (resultSet.next()) {
                String username = resultSet.getString("username");
                String password1 = resultSet.getString("password");
                int id1 = resultSet.getInt("id");
                String groups = resultSet.getString("groups");
                String name = resultSet.getString("name");
                String gender = resultSet.getString("gender");
                int id_card = resultSet.getInt("id_card");
                String phone = resultSet.getString("phone");
                String identity = resultSet.getString("identity");
                int book_count = resultSet.getInt("book_count");
                int state = resultSet.getInt("state");
                user = new User(id1,groups,name,username,password1,gender,id_card,phone,identity,book_count,state);
                userlist.add(user);
                System.out.println("已将"+count+"条数据加入uselist数组");

                count++;
            }

            for (User user1 : userlist) {
                if (user1.getUsername().equals(id) && user1.getPassword().equals(password)) {
                    System.out.println("登录成功");
                    new NoticeJFrame("登录成功!");
                    this.setVisible(false);
                    Lib_JPanel libPanel = new Lib_JPanel(user1); // 将 user1 对象传递给 Lib_JPanel
                    new Library_ManagementJFrame();
                    break;
                } else {
                    System.out.println("账号密码错误");
                    new NoticeJFrame("账号密码错误!");
                    break;
                }
            }


            resultSet.close();
            statement.close();
        } catch (ClassNotFoundException ex) {
            System.out.println("找不到 MySQL JDBC 驱动程序");
            ex.printStackTrace();
        } catch (SQLException ex) {
            System.out.println("数据库连接失败");
            ex.printStackTrace();
        }
    }
    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
